﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using acessaDB;
using Frame;
using System.Data;
using System.Configuration;
using FirebirdSql.Data.FirebirdClient;
using Estrutura;


namespace Banco
{

    public class BD_Aeronave_categoria
    {
        #region variaveis_globais

        acessaDB.conectaDBFireBird fireBird = new conectaDBFireBird();
        string conectionString = System.Configuration.ConfigurationManager.AppSettings["FireBirdConnectionLocal"];
        string usuario = string.Empty;

        #endregion

        #region construtor

        public BD_Aeronave_categoria()
        {
            if (System.Configuration.ConfigurationManager.AppSettings["ambiente"] == "PROD")
                conectionString = System.Configuration.ConfigurationManager.AppSettings["FireBirdConnectionProd"];
        }
        public BD_Aeronave_categoria(string usu_Login)
        {
            if (System.Configuration.ConfigurationManager.AppSettings["ambiente"] == "PROD")
                conectionString = System.Configuration.ConfigurationManager.AppSettings["FireBirdConnectionProd"];
            usuario = usu_Login;
        }

        #endregion

        #region metodos

        public DataSet _retorna_Aeronave_categoria(
                                                string ACFT_CATEGORIA_ID,
                                                string ACC_DESCRICAO,
                                                string ACC_DESCRICAO_LONGA,
                                                string USU_INSERT,
                                                string DATA_INSERT,
                                                string USU_UPDATE,
                                                string DATA_UPDATE
                                                )
        {

            if (string.IsNullOrEmpty(ACFT_CATEGORIA_ID)) ACFT_CATEGORIA_ID = "''";
            if (string.IsNullOrEmpty(ACC_DESCRICAO)) ACC_DESCRICAO = "''";
            if (string.IsNullOrEmpty(ACC_DESCRICAO_LONGA)) ACC_DESCRICAO_LONGA = "''";
            if (string.IsNullOrEmpty(USU_INSERT)) USU_INSERT = "'0'";
            if (string.IsNullOrEmpty(DATA_INSERT)) DATA_INSERT = "''";
            if (string.IsNullOrEmpty(USU_UPDATE)) USU_UPDATE = "'0'";
            if (string.IsNullOrEmpty(DATA_UPDATE)) DATA_UPDATE = "''";

            string sql = "SELECT " +
                                  "CTM_TAB_CATEGORIA_ACFT.ACFT_CATEGORIA_ID, " +
                                  "CTM_TAB_CATEGORIA_ACFT.ACC_DESCRICAO, " +
                                  "CTM_TAB_CATEGORIA_ACFT.ACC_DESCRICAO_LONGA, " +
                                  "CTM_TAB_CATEGORIA_ACFT.USU_INSERT, " +
                                  "CTM_TAB_CATEGORIA_ACFT.DATA_INSERT, " +
                                  "CTM_TAB_CATEGORIA_ACFT.USU_UPDATE, " +
                                  "CTM_TAB_CATEGORIA_ACFT.DATA_UPDATE " +
                            "FROM CTM_TAB_CATEGORIA_ACFT " +
                            //"LEFT JOIN USUARIO ON USUARIO.USUARIO_ID = CTM_TAB_CATEGORIA_ACFT.USUARIO_ID " +
                            //"LEFT JOIN USUARIO ON USUARIO.USUARIO_ID = CTM_TAB_CATEGORIA_ACFT.USUARIO_ID " +
                            "WHERE " +
                            "      (CTM_TAB_CATEGORIA_ACFT.ACFT_CATEGORIA_ID               = " + ACFT_CATEGORIA_ID + "  OR " + ACFT_CATEGORIA_ID + " = '') AND " +
                            "      (CTM_TAB_CATEGORIA_ACFT.ACC_DESCRICAO                   = " + ACC_DESCRICAO + "  OR " + ACC_DESCRICAO + " = '') AND " +
                            "      (CTM_TAB_CATEGORIA_ACFT.ACC_DESCRICAO_LONGA             = " + ACC_DESCRICAO_LONGA + "  OR " + ACC_DESCRICAO_LONGA + " = '') AND " +
                            "      (CTM_TAB_CATEGORIA_ACFT.USU_INSERT                      = " + USU_INSERT + "  OR " + USU_INSERT + " = '0') AND " +
                            "      (CTM_TAB_CATEGORIA_ACFT.DATA_INSERT                     = " + DATA_INSERT + "  OR " + DATA_INSERT + " = '') AND " +
                            "      (CTM_TAB_CATEGORIA_ACFT.USU_UPDATE                      = " + USU_UPDATE + "  OR " + USU_UPDATE + " = '0') AND " +
                            "      (CTM_TAB_CATEGORIA_ACFT.DATA_UPDATE                     = " + DATA_UPDATE + "  OR " + DATA_UPDATE + " = '') ";

            DataSet dsRetorno = fireBird.retornaDataSet(sql, conectionString);
            return dsRetorno;

        }


        public List<ES_Aeronave_categoria> _obter_Aeronave_categoria(
                                                string ACFT_CATEGORIA_ID,
                                                string ACC_DESCRICAO,
                                                string ACC_DESCRICAO_LONGA,
                                                string USU_INSERT,
                                                string DATA_INSERT,
                                                string USU_UPDATE,
                                                string DATA_UPDATE
                                                )
        {

            if (string.IsNullOrEmpty(ACFT_CATEGORIA_ID)) ACFT_CATEGORIA_ID = "''"; else ACFT_CATEGORIA_ID = Util.retonarAspas(ACFT_CATEGORIA_ID + " % ");
            if (string.IsNullOrEmpty(ACC_DESCRICAO)) ACC_DESCRICAO = "''"; else ACC_DESCRICAO = Util.retonarAspas(ACC_DESCRICAO + " % ");
            if (string.IsNullOrEmpty(ACC_DESCRICAO_LONGA)) ACC_DESCRICAO_LONGA = "''"; else ACC_DESCRICAO_LONGA = Util.retonarAspas(ACC_DESCRICAO_LONGA + " % ");
            if (string.IsNullOrEmpty(USU_INSERT)) USU_INSERT = "0";
            if (string.IsNullOrEmpty(DATA_INSERT)) DATA_INSERT = "''"; else DATA_INSERT = Util.retonarAspas(DATA_INSERT + " % ");
            if (string.IsNullOrEmpty(USU_UPDATE)) USU_UPDATE = "0";
            if (string.IsNullOrEmpty(DATA_UPDATE)) DATA_UPDATE = "''"; else DATA_UPDATE = Util.retonarAspas(DATA_UPDATE + " % ");

            string sql = "SELECT " +
                                  "CTM_TAB_CATEGORIA_ACFT.ACFT_CATEGORIA_ID, " +
                                  "CTM_TAB_CATEGORIA_ACFT.ACC_DESCRICAO, " +
                                  "CAST(CTM_TAB_CATEGORIA_ACFT.ACC_DESCRICAO_LONGA AS VARCHAR(2000)) AS ACC_DESCRICAO_LONGA, " +
                                  "CTM_TAB_CATEGORIA_ACFT.USU_INSERT, " +
                                  "CTM_TAB_CATEGORIA_ACFT.DATA_INSERT, " +
                                  "CTM_TAB_CATEGORIA_ACFT.USU_UPDATE, " +
                                  "CTM_TAB_CATEGORIA_ACFT.DATA_UPDATE " +
                            "FROM CTM_TAB_CATEGORIA_ACFT " +
                            "WHERE " +
                            "      (CTM_TAB_CATEGORIA_ACFT.ACFT_CATEGORIA_ID               = " + ACFT_CATEGORIA_ID + "  OR " + ACFT_CATEGORIA_ID + " = '') AND " +
                            "      (CTM_TAB_CATEGORIA_ACFT.ACC_DESCRICAO                   = " + ACC_DESCRICAO + "  OR " + ACC_DESCRICAO + " = '') AND " +
                            "      (CTM_TAB_CATEGORIA_ACFT.ACC_DESCRICAO_LONGA             = " + ACC_DESCRICAO_LONGA + "  OR " + ACC_DESCRICAO_LONGA + " = '') " +
                            " ORDER BY CTM_TAB_CATEGORIA_ACFT.ACC_DESCRICAO";
                            

            List<ES_Aeronave_categoria> lista = new List<ES_Aeronave_categoria>();
            FbConnection fbConexao = new FbConnection(conectionString);
            FbDataReader dr = fireBird.retornaDataReader(sql, conectionString, fbConexao);
            try
            {
                while (dr.Read())
                {
                    ES_Aeronave_categoria item = new ES_Aeronave_categoria();
                    item.ACFT_CATEGORIA_ID = dr["ACFT_CATEGORIA_ID"].ToString();
                    item.ACC_DESCRICAO = dr["ACC_DESCRICAO"].ToString();
                    item.ACC_DESCRICAO_LONGA = dr["ACC_DESCRICAO_LONGA"].ToString();
                    item.USU_INSERT = dr["USU_INSERT"].ToString();
                    item.DATA_INSERT = dr["DATA_INSERT"].ToString();
                    item.USU_UPDATE = dr["USU_UPDATE"].ToString();
                    item.DATA_UPDATE = dr["DATA_UPDATE"].ToString();
                    lista.Add(item);

                }
                return lista;
            }
            finally
            {
                fireBird.fecharConexao(fbConexao);
            }
        }


        public ES_Aeronave_categoria _obter_Aeronave_categoria_by_id(string ACFT_CATEGORIA_ID)
        {
            if (string.IsNullOrEmpty(ACFT_CATEGORIA_ID)) ACFT_CATEGORIA_ID = "'0'";

            string sql = "SELECT " +
                                  "CTM_TAB_CATEGORIA_ACFT.ACFT_CATEGORIA_ID, " +
                                  "CTM_TAB_CATEGORIA_ACFT.ACC_DESCRICAO, " +
                                  "CTM_TAB_CATEGORIA_ACFT.ACC_DESCRICAO_LONGA, " +
                                  "CTM_TAB_CATEGORIA_ACFT.USU_INSERT, " +
                                  "CTM_TAB_CATEGORIA_ACFT.DATA_INSERT, " +
                                  "CTM_TAB_CATEGORIA_ACFT.USU_UPDATE, " +
                                  "CTM_TAB_CATEGORIA_ACFT.DATA_UPDATE " +
                            "FROM CTM_TAB_CATEGORIA_ACFT " +                            
                            "WHERE " +
                            "      (CTM_TAB_CATEGORIA_ACFT.ACFT_CATEGORIA_ID = " + ACFT_CATEGORIA_ID + " ) ";

            ES_Aeronave_categoria item = new ES_Aeronave_categoria();
            FbConnection fbConexao = new FbConnection(conectionString);
            FbDataReader dr = fireBird.retornaDataReader(sql, conectionString, fbConexao);
            try
            {
                while (dr.Read())
                {
                    item.ACFT_CATEGORIA_ID = dr["ACFT_CATEGORIA_ID"].ToString();
                    item.ACC_DESCRICAO = dr["ACC_DESCRICAO"].ToString();
                    item.ACC_DESCRICAO_LONGA = dr["ACC_DESCRICAO_LONGA"].ToString();
                    item.USU_INSERT = dr["USU_INSERT"].ToString();
                    item.DATA_INSERT = dr["DATA_INSERT"].ToString();
                    item.USU_UPDATE = dr["USU_UPDATE"].ToString();
                    item.DATA_UPDATE = dr["DATA_UPDATE"].ToString();

                }
                return item;
            }
            finally
            {
                fireBird.fecharConexao(fbConexao);
            }
        }


        public void _inserir_Aeronave_categoria(
                                                string ACFT_CATEGORIA_ID,
                                                string ACC_DESCRICAO,
                                                string ACC_DESCRICAO_LONGA,
                                                string USU_INSERT,
                                                string DATA_INSERT,
                                                string USU_UPDATE,
                                                string DATA_UPDATE
                                                )
        {

            string sql = " INSERT INTO CTM_TAB_CATEGORIA_ACFT " +
                         "      ( " +
                         "          ACFT_CATEGORIA_ID              , " +
                         "          ACC_DESCRICAO                  , " +
                         "          ACC_DESCRICAO_LONGA            , " +
                         "          USU_INSERT                     , " +
                         "          DATA_INSERT                    , " +
                         "          USU_UPDATE                     , " +
                         "          DATA_UPDATE                      " +
                 "      )" +
                 "  VALUES " +
                 "      ( " +
                              Util.retonarAspas(ACFT_CATEGORIA_ID, true) + ", " +
                              Util.retonarAspas(ACC_DESCRICAO, true) + ", " +
                              Util.retonarAspas(ACC_DESCRICAO_LONGA, true) + ", " +
                              Util.retonarAspas(USU_INSERT, true) + ", " +
                              Util.retonarAspas(DATA_INSERT, true) + ", " +
                              Util.retonarAspas(USU_UPDATE, true) + ", " +
                              Util.retonarAspas(DATA_UPDATE, true) +
                 "      )";
            try
            {
                fireBird._ExecutarComandoRetornandoID(sql, conectionString, false, usuario);
            }
            catch
            { }
        }



        public void _atualizar_Aeronave_categoria(
                                                string ACFT_CATEGORIA_ID,
                                                string ACC_DESCRICAO,
                                                string ACC_DESCRICAO_LONGA,
                                                string USU_INSERT,
                                                string DATA_INSERT,
                                                string USU_UPDATE,
                                                string DATA_UPDATE
                                                )
        {

            string sql = " UPDATE CTM_TAB_CATEGORIA_ACFT SET " +
                         "      ACFT_CATEGORIA_ID               = " + Util.retonarAspas(ACFT_CATEGORIA_ID, true) + ", " +
                         "      ACC_DESCRICAO                   = " + Util.retonarAspas(ACC_DESCRICAO, true) + ", " +
                         "      ACC_DESCRICAO_LONGA             = " + Util.retonarAspas(ACC_DESCRICAO_LONGA, true) + ", " +
                         "      USU_INSERT                      = " + Util.retonarAspas(USU_INSERT, true) + ", " +
                         "      DATA_INSERT                     = " + Util.retonarAspas(DATA_INSERT, true) + ", " +
                         "      USU_UPDATE                      = " + Util.retonarAspas(USU_UPDATE, true) + ", " +
                         "      DATA_UPDATE                     = " + Util.retonarAspas(DATA_UPDATE, true) +
                         " WHERE ACFT_CATEGORIA_ID =  " + ACFT_CATEGORIA_ID;
            try
            {
                fireBird._ExecutarComandoRetornandoID(sql, conectionString, false, usuario);
            }
            catch
            { }
        }



        public void _excluir_Aeronave_categoria(string ACFT_CATEGORIA_ID)
        {
            string sql = "DELETE FROM CTM_TAB_CATEGORIA_ACFT WHERE ACFT_CATEGORIA_ID = " + ACFT_CATEGORIA_ID;
            try
            {
                fireBird._ExecutarComandoRetornandoID(sql, conectionString, false, usuario);
            }
            catch
            { }
        }



        #endregion
    }
}
